package com.edu.servlet;

import com.edu.bean.StudentScore;
import com.edu.bean.User;
import com.edu.service.UserScoreService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/admin/score/*")
public class AdminScoreServlet extends HttpServlet {
    private UserScoreService userScoreService = new UserScoreService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if (pathInfo == null) {
            pathInfo = "/";
        }

        switch (pathInfo) {
            case "/my":
                listMyScores(request, response);
                break;
            default:
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    private void listMyScores(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前登录用户
        User user = (User) request.getSession().getAttribute("adminUser");
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/admin/login.jsp");
            return;
        }

        // 获取分页参数
        int pageSize = 10;
        int currentPage = 1;
        String pageStr = request.getParameter("page");
        if (pageStr != null && !pageStr.trim().isEmpty()) {
            try {
                currentPage = Integer.parseInt(pageStr);
                if (currentPage < 1) {
                    currentPage = 1;
                }
            } catch (NumberFormatException e) {
                // 使用默认值
            }
        }

        // 获取筛选参数
        Integer subjectId = null;
        String subjectIdStr = request.getParameter("subjectId");
        if (subjectIdStr != null && !subjectIdStr.trim().isEmpty()) {
            try {
                subjectId = Integer.parseInt(subjectIdStr);
            } catch (NumberFormatException e) {
                // 使用默认值
            }
        }

        // 查询数据
        Map<String, Object> result = userScoreService.userListScores(user.getUserId(), subjectId, currentPage, pageSize);
        List<StudentScore> scoreList = (List<StudentScore>) result.get("list");
        int total = (Integer) result.get("total");
        int totalPages = (total + pageSize - 1) / pageSize;

        // 获取所有学科列表（用于筛选）
        request.setAttribute("subjectList", userScoreService.userListSubjects());
        request.setAttribute("scoreList", scoreList);
        request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalPages", totalPages);
        request.setAttribute("totalCount", total);

        request.getRequestDispatcher("/admin/my-score-list.jsp").forward(request, response);
    }
}